package org.leyi.shop.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.leyi.shop.common.exception.BusinessException;
import org.leyi.shop.common.response.BaseResponse;
import org.leyi.shop.common.response.EnumResponse;
import org.leyi.shop.model.request.UserModifyPasswordRequest;
import org.leyi.shop.model.request.UserRequest;
import org.leyi.shop.model.vo.SafetyUser;
import org.leyi.shop.service.UserService;
import org.leyi.shop.util.BaseResponseUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * description:  用户视图
 * @author  lenovo
 * create 2024/5/6-22:08 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     *
     * description: 用户注册
     * @param userRequest 用户注册需要的参数
     * @return BaseResponse<Integer>
     * @author  lenovo
     * create 2024/5/6-22:56
     */
    @PostMapping("")
    public BaseResponse<Long> register(@RequestBody UserRequest userRequest)
    {
        String userPassword= userRequest.getUserPassword();
        String userAccount=userRequest.getUserAccount();
        return BaseResponseUtils.success(userService.register(userAccount,userPassword),"注册成功");
    }

    /**
     *
     * description: 获得登录态
     * @return BaseResponse<SafetyUser>
     * @author  lenovo
     * create 2024/5/6-22:59
     */
    @GetMapping("/get/self")
    public BaseResponse<SafetyUser> getSelf()
    {
        return BaseResponseUtils.success(userService.getCurrentUser(),"获取登录态成功");
    }
    /**
     *
     * description: 用户登录
     * @param userRequest 用户登录的参数
     * @return BaseResponse<SafetyUser>
     * @author  lenovo
     * create 2024/5/6-22:56
     */
    @PostMapping("/login")
    public BaseResponse<SafetyUser> login(@RequestBody UserRequest userRequest, HttpServletRequest request)
    {
        String userPassword= userRequest.getUserPassword();
        String userAccount=userRequest.getUserAccount();
        return BaseResponseUtils.success(userService.login(userAccount,userPassword,request),"登录成功");
    }

    /**
     *
     * description: 用户修改非敏感信息
     * @param safetyUser 从前端修改用户所需要的参数
     * @return BaseResponse<SafetyUser>
     * @author  lenovo
     * create 2024/5/9-14:56
     */
    @PutMapping()
    public BaseResponse<SafetyUser> updateUserInfo(@RequestBody SafetyUser safetyUser)
    {
        if(safetyUser==null)
        {
            throw new BusinessException(EnumResponse.PARAMETER_ERR);
        }
        return BaseResponseUtils.success(userService.modifyUser(safetyUser),"修改信息成功");
    }

    /**
     *
     * description: 查询用户
     * @param userId 用户id
     * @return BaseResponse<List<SafetyUser>>
     * @author  lenovo
     * create 2024/5/9-15:03
     */
    @GetMapping("find/by/id/{userId}")
    public BaseResponse<List<SafetyUser>> searchByUserId(@PathVariable Long userId)
    {
       return BaseResponseUtils.success(userService.searchUserById(userId),"根据id查找用户成功") ;
    }
    /**
     *
     * description: 查询用户
     * @param userName 用户名
     * @return BaseResponse<List<SafetyUser>>
     * @author  lenovo
     * create 2024/5/9-15:03
     */
    @GetMapping("find/by/name/{userName}")
    public BaseResponse<List<SafetyUser>> searchUserByName(@PathVariable String userName)
    {
        return BaseResponseUtils.success(userService.searchUserByName(userName),"根据用户名查找用户成功") ;
    }

    /**
     *
     * description: 查找用户
     * @param userAccount 用户账户
     * @return BaseResponse<List<SafetyUser>>
     * @author  lenovo
     * create 2024/5/9-15:03
     */
    @GetMapping("find/by/account/{userAccount}")
    public BaseResponse<List<SafetyUser>> searchUserByAccount(@PathVariable String userAccount)
    {
        return BaseResponseUtils.success(userService.searchUserByAccount(userAccount),"根据账号查找用户成功") ;
    }

    /**
     *
     * description: 用户登出接口
     * @return BaseResponse<String>
     * @author  lenovo
     * create 2024/5/9-15:31
     */
    @GetMapping("logout")
    public BaseResponse<String> logout(HttpServletRequest request)
    {userService.logout(request);
        return BaseResponseUtils.success(null,"退出登录成功") ;
    }

    /**
     *
     * description: 超管赋予管理员权限
     * @param userId userID
     * @return BaseResponse<String>
     * @author  lenovo
     * create 2024/5/9-16:20
     */
    @GetMapping("/promotion/{userId}")
    public BaseResponse<String> promotion(@PathVariable Long userId)
    { userService.modifyUserRole(userId,1);
        return BaseResponseUtils.success("晋升成功");
    }

    /**
     *
     * description: 超管将管理员剔除
     * @param userId 用户id
     * @return BaseResponse<String>
     * @author  lenovo
     * create 2024/5/9-16:20
     */
    @GetMapping("/demotion/{userId}")
    public BaseResponse<String> demotion(@PathVariable Long userId)
    { userService.modifyUserRole(userId,0);
        return BaseResponseUtils.success("降级成功");
    }

    /**
     *
     * description: 用户修改密码
     * @param userModifyPasswordRequest 修改密码
     * @return BaseResponse<String>
     * @author  lenovo
     * create 2024/5/9-16:27
     */
    @PostMapping("modify/password")
    public BaseResponse<String> modifyPassword(@RequestBody UserModifyPasswordRequest userModifyPasswordRequest)
    {
        userService.modifyUserPassword(userModifyPasswordRequest.getId(),
                userModifyPasswordRequest.getOldPassword(), userModifyPasswordRequest.getNewPassword());
        return BaseResponseUtils.success("修改密码成功了");
    }


}
